﻿Public Class HostHelper
    Private ReadOnly _hostIp As Byte
    Private ReadOnly _userName As String
    Private ReadOnly _messageQueue As Queue(Of MensajeBroadcasted)
    Private _lastPing As Date
    Private _dateOfLastMessage As Date
    Private _boletoLoteriaTurno As Integer

    Sub New(hostIp As Byte, ByVal userName As String)
        _hostIp = hostIp
        _userName = userName
        _lastPing = Date.Now
        _messageQueue = New Queue(Of MensajeBroadcasted)()
    End Sub

    Public ReadOnly Property HostIp() As Byte
        Get
            Return _hostIp
        End Get
    End Property

    ''' <summary>
    ''' Este es el ID único que identifica a cada equipo de la red
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IDHost As String
        Get
            Return String.Format("{0:000}{1}", _hostIp, _userName)
        End Get
    End Property

    ''' <summary>
    ''' Devuelve el nombre del usuario. Sólo debería utilizarse de forma informativa.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property UserName As String
        Get
#If DEBUG Then
            Return IDHost
#Else
            Return _userName
#End If
        End Get
    End Property

    Public ReadOnly Property BoletoLoteriaTurno() As Integer
        Get
            Return _boletoLoteriaTurno
        End Get
    End Property


    ''' <summary>
    ''' Debe ser llamado cuando un host ha realizado un broadcast con su nombre y sin ningún mensaje específico
    ''' Sirve para poder hacer la lista de los que hay disponibles
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub PingReceived(numeroAlAzar As Integer)
        _lastPing = Date.Now
        _boletoLoteriaTurno = numeroAlAzar
    End Sub

    Public Sub MessageReceived(mb As MensajeBroadcasted)
        While (_messageQueue.Count > 50) ' prevent memory leaks
            _messageQueue.Dequeue()
        End While
        _dateOfLastMessage = Date.Now
        _messageQueue.Enqueue(mb)
    End Sub

    Public Function PreviewMensaje() As MensajeBroadcasted
        If _messageQueue.Count = 0 Then Return Nothing
        Return _messageQueue.Peek()
    End Function

    Public Function DequeueMensaje() As MensajeBroadcasted
        If _messageQueue.Count = 0 Then Return Nothing
        Return _messageQueue.Dequeue()
    End Function

    Public ReadOnly Property DateOfLastMessage As Date
        Get
            Return _dateOfLastMessage
        End Get
    End Property

    Public ReadOnly Property IsSearchingForEnemy As Boolean
        Get
            If Math.Floor(Now.Subtract(_lastPing).TotalSeconds) < 6 Then
                Return True
            End If
            Return False
        End Get
    End Property

    Public Shared Operator =(ByVal left As HostHelper, ByVal right As HostHelper) As Boolean
        Return left.IDHost = right.IDHost
    End Operator

    Public Shared Operator <>(ByVal left As HostHelper, ByVal right As HostHelper) As Boolean
        Return left.IDHost <> right.IDHost
    End Operator
End Class